<template>
  <div class="app-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1>服务列表</h1>
      <p class="header-desc">选择您需要的服务，查看详细信息</p>
    </div>

    <!-- 遍历父类别数组 -->
    <div
        v-for="(category, index) in responseData"
        :key="category.categoryId"
        class="category-wrapper"
    >
      <!-- 父类别名称（带图标装饰） -->
      <div class="category-header">
        <h2 class="category-title">
          <i class="category-icon" :class="getCategoryIcon(index)"></i>
          {{ category.categoryName }}
        </h2>
        <div class="service-count">共 {{ category.services.length }} 项服务</div>
      </div>

      <!-- 子服务列表（网格布局） -->
      <div class="service-list">
        <!-- 子服务项（卡片式设计） -->
        <div
            v-for="(service, serviceIndex) in category.services"
            :key="service.serviceId"
            class="service-item"
        >
          <router-link
              :to="`/service/details/${service.serviceId}`"
              class="service-link"
          >
            <!-- 服务图标 -->
            <div class="service-icon">
              <!-- 正确写法：使用模板字符串或变量拼接 -->
              <img :src="'http://47.120.29.84:8081' + service.serviceHeadImg"  alt="服务图标" >

            </div>
            <!-- 服务信息 -->
            <div class="service-info">
              <h3 class="service-mainname">{{ service.serviceMainName }}</h3>


              <!-- 服务标签（如适用范围缩写） -->

            </div>

            <!-- 箭头图标 -->
            <div class="service-arrow">
              <i class="iconfont icon-arrow-right"></i>
            </div>
          </router-link>
        </div>
      </div>
    </div>

    <!-- 加载状态和空状态 -->
    <div v-if="responseData.length === 0 && isLoading" class="loading-state">
      <div class="spinner"></div>
      <p>加载服务中...</p>
    </div>
    <div v-if="responseData.length === 0 && !isLoading" class="empty-state">
      <i class="iconfont icon-empty"></i>
      <p>暂无服务数据</p>
    </div>
  </div>
</template>

<script>
import {BASE_URL} from "@/util/Config.js";
export default {
  data() {
    return {
      responseData: [],
      isLoading: true // 加载状态标记
    };
  },
  methods: {
    BASE_URL() {
      return BASE_URL
    },

    async sendData() {
      try {
        this.isLoading = true; // 开始加载
        const response = await fetch(BASE_URL+'/services', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ categoryPid:4})
        });

        if (!response.ok) throw new Error(`请求失败：${response.status}`);
        const result = await response.json();
        this.responseData = result.data || [];

      } catch (error) {
        console.error('获取数据失败：', error);
        this.responseData = [];
      } finally {
        this.isLoading = false; // 结束加载
      }
    },
    // 根据类别索引返回不同图标类名（增加视觉区分）
    getCategoryIcon(index) {
      const icons = ['icon-category-1', 'icon-category-2', 'icon-category-3', 'icon-category-4'];
      return icons[index % icons.length];
    }
  },
  mounted() {
    this.sendData();
    console.log(this.responseData);
  }
};
</script>

<style scoped>
/* 基础样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.app-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px 30px;
}

/* 页面标题 */
.page-header {
  margin: 0 0 30px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}
.page-header h1 {
  color: #2c3e50;
  font-size: 24px;
  margin-bottom: 8px;
}
.header-desc {
  color: #666;
  font-size: 14px;
}

/* 类别容器 */
.category-wrapper {
  margin-bottom: 40px;
  padding: 25px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s, box-shadow 0.3s;
}
.category-wrapper:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
}

/* 类别头部 */
.category-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.category-title {
  display: flex;
  align-items: center;
  color: #1a1a1a;
  font-size: 18px;
  font-weight: 600;
}
.category-icon {
  margin-right: 10px;
  font-size: 20px;
  color: #ff9900;
}
.service-count {
  color: #888;
  font-size: 14px;
  background: #f5f5f5;
  padding: 3px 10px;
  border-radius: 12px;
}

/* 服务列表（网格布局） */
.service-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 18px;
}

/* 服务项（卡片式） */
.service-item {
  background: #fff;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s;
}
.service-item:hover {
  border-color: #ff9900;
  box-shadow: 0 4px 12px rgba(255, 153, 0, 0.1);
  transform: translateY(-3px);
}

.service-link {
  display: flex;
  padding: 15px;
  color: inherit;
  text-decoration: none;
}

/* 服务图标 */
.service-icon {
  width: 48px;
  height: 48px;
  background: #fff8e6;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  flex-shrink: 0;
}
.service-icon .icon-service {
  font-size: 24px;
  color: #ff9900;
}

/* 服务信息 */
.service-info {
  flex-grow: 1;
  min-width: 0; /* 解决文字溢出 */
}
.service-mainname {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.service-subname {
  color: #666;
  font-size: 13px;
  margin-bottom: 8px;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* 服务标签 */
.service-tags {
  margin-top: auto;
}
.tag {
  display: inline-block;
  color: #888;
  font-size: 12px;
  background: #f9f9f9;
  padding: 2px 8px;
  border-radius: 4px;
}

/* 箭头图标 */
.service-arrow {
  display: flex;
  align-items: center;
  color: #ccc;
  font-size: 16px;
  margin-left: 5px;
  transition: color 0.3s;
}
.service-item:hover .service-arrow {
  color: #ff9900;
}

/* 加载状态 */
.loading-state {
  text-align: center;
  padding: 60px 0;
}
.spinner {
  width: 40px;
  height: 40px;
  margin: 0 auto;
  border: 4px solid #f0f0f0;
  border-top: 4px solid #ff9900;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 60px 0;
  color: #999;
}
.empty-state .icon-empty {
  font-size: 48px;
  margin-bottom: 15px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .app-container {
    padding: 15px;
  }
  .category-wrapper {
    padding: 15px;
    margin-bottom: 25px;
  }
  .service-list {
    grid-template-columns: 1fr;
  }
}
</style>